/**
 * 
 */
package org.t2.game.server.module.marquee.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.t2.design.module.MarqueeTable;
import org.t2.design.module.autogen.MarqueeAbstractTable.MarqueeCfg;
import org.t2.game.server.foundation.player.PlayerContextManager;
import org.t2.game.server.module.player.model.Player;
import org.t2.game.server.module.player.service.PlayerService;

import pb.t2.protobuf.marquee.Marquee.MarqueePushMessage;

/**
 * @author YY
 */
@Service
public class MarqueeService {

	private static Logger logger = LoggerFactory.getLogger(MarqueeService.class);
	
	@Autowired
	private PlayerService playerService;
	@Autowired
	private PlayerContextManager playerContextManager;
	
	
	public void push(int template, Object... args) {
		MarqueeCfg marqueeCfg = MarqueeTable.ins().findInIdIndex(template);
		if(marqueeCfg == null) {
			logger.error("未找到ID为" + template + "跑马灯模板");
			return;
		}
		MarqueePushMessage.Builder message = MarqueePushMessage.newBuilder();
		message.setTemplate(template);
		message.addAllArgs(convertArgs(marqueeCfg.getServerRules(), args));
		playerContextManager.pushAll(MarqueePushMessage.Proto.ID, message.build());
	}
	
	public List<String> convertArgs(List<Integer> rules, Object... args) {
		List<String> resultArgs = new ArrayList<>();
		for(int i = 0; i < rules.size() && i < args.length; i++) {
			int rule = rules.get(i);
			String arg = args[i].toString();
			if(rule == 0) {
				resultArgs.add(arg);
			} else if(rule == 1) {
				long playerId = Long.parseLong(arg);
				Player player = playerService.getEntity(playerId);
				resultArgs.add(player.getNick());
			} else if(rule == 2) {
				resultArgs.add(arg);
			} else if(rule == 3) {
				resultArgs.add(arg);
			}
		}
		return resultArgs;
	}
}
